/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.stat.content.log;

import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.NormalizedMessage;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.stat.StatMessages;

import java.util.Iterator;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class ViewRequestPanel extends Composite {

	private StatMessages cons = null;

	private BifangGWTServiceAsync service = null;

	private GwtContext context;

	private VerticalPanel panel;
	

	private DialogBox box;
	
	private boolean loaded;
	

	// private HashMap<String,String> gIds;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			panel.clear();
			NormalizedMessage m = (NormalizedMessage) res.getResponse();
			if (m == null)
				panel.add(new HTML(cons.noData()));
			else{
				Grid table = new Grid(m.getHeads().size() + 1 , 2);
				
				table.setWidth("70%");
				table.setCellPadding(1);
				table.setCellSpacing(1);
				table.setBorderWidth(1);

				ColumnFormatter cf = table.getColumnFormatter();

				cf.setWidth(0, "50%");
				cf.setWidth(1, "50%");
				
				CellFormatter ff = table.getCellFormatter();
				ff.setStyleName(0, 0, "tablecol");
				ff.setStyleName(0, 1, "tablecol");
				
				table.setHTML(0, 0, cons.header());
				table.setHTML(0, 1, cons.value());
				
				Iterator it = m.getHeads().keySet().iterator();
				int i = 1;
				while(it.hasNext()){
					String s = (String)it.next();
					table.setHTML(i, 0, s);
					table.setHTML(i, 1, m.getHead(s));
					i++;
				}
				
				panel.add(table);
				panel.add(new HTML(cons.body()));
				TextArea resp = new TextArea();
				resp.setCharacterWidth(60);
				resp.setVisibleLines(9);
				resp.setText(m.getBody());
				panel.add(resp);
				
			}
			HorizontalPanel hp = new HorizontalPanel();
			hp.setSpacing(10);
			Button re = new Button(cons.close());
			re.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					box.hide();

				}
			});

			hp.add(re);

			

			panel.add(hp);
		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public ViewRequestPanel(StatMessages cons, BifangGWTServiceAsync service,
			GwtContext ctx, DialogBox box1) {
		this.service = service;
		this.cons = cons;
		this.box = box1;
		this.context = ctx;
		panel = new VerticalPanel();
		panel.setWidth("100%");
		// gIds = new HashMap<String, String>();
		//		
		// for(Usergroup ug : gs){
		// gIds.put(ug.getId(), "");
		// }
		

		//panel.add(req);

		
		initWidget(panel);

	}

	public void onLoad() {
		if(loaded)
			return;
		RequestObject req = new RequestObject();
		req.setAction("han.bifang.gwt.server.action.stat.LogDetailAction");
		req.setRequest("request");
		service.call(req, cb);
		loaded = true;
	}

}
